﻿using MediatR;
using S.Produce.Api.Application.Commands.HistoryCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.HistoryCommandHandlers
{
    public class HIstoryDeleteCommandHandler : IRequestHandler<HIstoryDeleteCommand, int>
    {
        private readonly IRepository<History> repository;

        public HIstoryDeleteCommandHandler(IRepository<History> repository)
        {
            this.repository = repository;
        }

        public async Task<int> Handle(HIstoryDeleteCommand request, CancellationToken cancellationToken)
        {
            var Ids = request.Ids.Split(",");
            var count = 0;
            foreach (var item in Ids)
            {
                var del = await repository.GetAsync(int.Parse(item));
                del.IsDel = true;
                count += await repository.UpdateAsync(del);
            }
            return count;
        }
    }
}
